package com.YCH.consummer.service.impl;

import com.YCH.consummer.service.MailService;
import com.YCH.model.user.vo.MailVo;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import java.util.Date;

/**
 * Create bySeptember
 * 2019/4/7
 * 21:42
 *
 * @author 16554
 */
@Service
@Log4j2
public class MailServiceImpl implements MailService {
    //注入邮件工具类
    @Autowired
    private JavaMailSenderImpl mailSender;
    @Override
    public MailVo sendMail(MailVo mailVo) {
        try {
            //1.检测邮件
            checkMail(mailVo);
            //2.发送邮件
            sendMimeMail(mailVo);
            //3.保存邮件
            return saveMail(mailVo);
        } catch (Exception e) {
            mailVo.setStatus("fail");
            mailVo.setError(e.getMessage());
            return mailVo;
        }
    }

    // 检测邮件信息类
    private void checkMail(MailVo mailVo) {
        if (StringUtils.isEmpty(mailVo.getTo())) {
            throw new RuntimeException("邮件收信人不能为空");
        }
        if (StringUtils.isEmpty(mailVo.getSubject())) {
            throw new RuntimeException("邮件主题不能为空");
        }
        if (StringUtils.isEmpty(mailVo.getText())) {
            throw new RuntimeException("邮件内容不能为空");
        }
    }

    // 构建复杂邮件信息类
    @Async
    void sendMimeMail(MailVo mailVo) {
        try {
            // true表示支持复杂类型
            MimeMessageHelper messageHelper = new MimeMessageHelper(mailSender.createMimeMessage(), true);
            // 邮件发信人从配置项读取
            // mailVo.setFrom(getMailSendFrom());
            // 邮件发信人
            messageHelper.setFrom(mailVo.getFrom());
            // 邮件收信人
            messageHelper.setTo(mailVo.getTo().split(","));
            // 邮件主题
            messageHelper.setSubject(mailVo.getSubject());
            if(mailVo.getIsHtml()==null){
                mailVo.setIsHtml(false);
            }
            // 邮件内容 参数一：内容 参数二；是否是html
            messageHelper.setText(mailVo.getText(),mailVo.getIsHtml());
            // 抄送
            if (!StringUtils.isEmpty(mailVo.getCc())) {
                messageHelper.setCc(mailVo.getCc().split(","));
            }
            // 密送
            if (!StringUtils.isEmpty(mailVo.getBcc())) {
                messageHelper.setCc(mailVo.getBcc().split(","));
            }
            // 添加邮件附件
            if (mailVo.getMultipartFiles() != null) {
                for (MultipartFile multipartFile : mailVo.getMultipartFiles()) {
                    messageHelper.addAttachment(multipartFile.getOriginalFilename(), multipartFile);
                }
            }
            // 发送时间
            if (StringUtils.isEmpty(mailVo.getSentDate())) {
                mailVo.setSentDate(new Date());
                messageHelper.setSentDate(mailVo.getSentDate());
            }
            // 正式发送邮件
            mailSender.send(messageHelper.getMimeMessage());
            mailVo.setStatus("ok");
        } catch (Exception e) {
            // 发送失败
            log.error("邮件发送失败！邮件发送人"+mailVo.getFrom()+",邮件接收"+mailVo.getTo()+",邮件主题:"+mailVo.getSubject()+",邮件内容:"+mailVo.getText());
            throw new RuntimeException(e);
        }
    }

    // 保存邮件
    private MailVo saveMail(MailVo mailVo) {
        //将邮件保存到数据库..
        return mailVo;
    }

    // 获取邮件发信人
    public String getMailSendFrom() {
        return mailSender.getJavaMailProperties().getProperty("from");
    }

}